package com.ekuaitu.kuaitustaff.baiduMap;


import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.ekuaitu.kuaitustaff.baiduMap.RoutePlanOverly.DrivingRouteOverlay;
import com.ekuaitu.kuaitustaff.baiduMap.RoutePlanOverly.WalkingRouteOverlay;


/**
 * Created by Michael on 11/6.
 */

public class RoutePlan {
    private BaiduMap mBaiduMap;
    private RoutePlanSearch mSearch;
    private OnRoutePlannedListener onRoutePlannedListener;
    private WalkingRouteOverlay walkOverlay;
    private DrivingRouteOverlay drivingRouteOverlay;
    private PlanNode startNode, endNode;
    private static RoutePlan instance;
    public static final int PLAN_WALK = 1;
    public static final int PLAN_DRIVE = 2;


    public static RoutePlan getInstance() {
        if (instance == null) {
            instance = new RoutePlan();
        }
        return instance;
    }

    public RoutePlan() {

    }


    /**
     * 步行路线规划
     *
     * @param curPosition
     * @param tarPosition
     * @param onRoutePlannedListener
     */
    public void startRoutePlan(BaiduMap mBaiduMap, int planType, LatLng curPosition, LatLng tarPosition, OnRoutePlannedListener onRoutePlannedListener) {
        this.mBaiduMap = mBaiduMap;
        mSearch = RoutePlanSearch.newInstance();
        this.onRoutePlannedListener = onRoutePlannedListener;
        mSearch = RoutePlanSearch.newInstance();
        startNode = PlanNode.withLocation(curPosition);
        endNode = PlanNode.withLocation(tarPosition);
        mSearch.setOnGetRoutePlanResultListener(listener);
        if (planType == PLAN_WALK) {
            mSearch.walkingSearch((new WalkingRoutePlanOption())  //发起路线规划
                    .from(startNode)
                    .to(endNode));
        } else if (planType == PLAN_DRIVE) {
            mSearch.drivingSearch((new DrivingRoutePlanOption())  //发起路线规划
                    .from(startNode)
                    .to(endNode));
        }

    }

    OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
        @Override
        public void onGetWalkingRouteResult(WalkingRouteResult result) {  //获得步行路线规划结果
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                if (onRoutePlannedListener != null) {
                    onRoutePlannedListener.onRoutePlanFailure();
                }
            }

            if (result != null && result.error == SearchResult.ERRORNO.NO_ERROR) {
                if (onRoutePlannedListener != null) {
                    onRoutePlannedListener.onRoutePlanSuccess(result.getRouteLines().get(0).getDistance(),
                            result.getRouteLines().get(0).getDuration());
                }
                walkOverlay = new WalkingRouteOverlay(mBaiduMap);
                walkOverlay.setData(result.getRouteLines().get(0));
                walkOverlay.addToMap();
                walkOverlay.zoomToSpan();

            }
        }

        @Override
        public void onGetTransitRouteResult(TransitRouteResult result) {

        }

        @Override
        public void onGetMassTransitRouteResult(MassTransitRouteResult result) {

        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult result) {
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                if (onRoutePlannedListener != null) {
                    onRoutePlannedListener.onRoutePlanFailure();
                }
            }

            if (result != null && result.error == SearchResult.ERRORNO.NO_ERROR) {
                if (onRoutePlannedListener != null) {
                    onRoutePlannedListener.onRoutePlanSuccess(result.getRouteLines().get(0).getDistance(),
                            result.getRouteLines().get(0).getDuration());
                }
                drivingRouteOverlay = new DrivingRouteOverlay(mBaiduMap);
                drivingRouteOverlay.setData(result.getRouteLines().get(0));
                drivingRouteOverlay.addToMap();
                drivingRouteOverlay.zoomToSpan();

            }
        }

        @Override
        public void onGetIndoorRouteResult(IndoorRouteResult result) {

        }

        @Override
        public void onGetBikingRouteResult(BikingRouteResult result) {

        }
    };

    public void clearRoutePlanning() {
        if (walkOverlay != null) {
            walkOverlay.removeFromMap();
            walkOverlay = null;
        }
    }

    public void clearDrvingRoutePlanning() {
        if (drivingRouteOverlay != null) {
            drivingRouteOverlay.removeFromMap();
            drivingRouteOverlay = null;
        }
    }

    public interface OnRoutePlannedListener {
        /**
         * @param distance 米
         * @param duration 秒
         */
        void onRoutePlanSuccess(int distance, int duration);

        void onRoutePlanFailure();
    }
}
